﻿using Behaving;
using Behaving.Actions;
using FluentAssertions;
using NUnit.Framework;

namespace Behaving.Tests
{
    [TestFixture]
    public class BasicTests
    {
        [Test]
        public void Can_create_behaviour_tree()
        {
            BehaviourTree behaviourTree = new BehaviourTree();
        }

        [Test]
        public void When_starting_a_new_task_that_ends_in_failure()
        {
            var executeWasRun = false;
            var startWasRun = false;
            var stopWasRun = false;
            BehaviourTree behaviourTree = new BehaviourTree();
            behaviourTree.Root = new ActionBehaviour(
                () =>
                {
                    executeWasRun = true;
                    return RunningState.Failure;
                },
                () =>
                {
                    startWasRun = true;
                },
                () =>
                {
                    stopWasRun = true;
                });
                        
            behaviourTree.Tick();
            executeWasRun.Should().BeTrue();
            startWasRun.Should().BeTrue();
            stopWasRun.Should().BeTrue();
        }

        [Test]
        public void When_continuing_a_running_task()
        {
            var executeWasRun = false;
            var startWasRun = false;
            var stopWasRun = false;
            BehaviourTree behaviourTree = new BehaviourTree();
            behaviourTree.Root = new ActionBehaviour(
                () =>
                {
                    executeWasRun = true;
                    return RunningState.Running;
                },
                () =>
                {
                    startWasRun = true;
                },
                () =>
                {
                    stopWasRun = true;
                });
            behaviourTree.SetWasRunningPreviousTick(behaviourTree.Root);
            behaviourTree.Tick();
            executeWasRun.Should().BeTrue();
            startWasRun.Should().BeFalse();
            stopWasRun.Should().BeFalse();
        }

        [Test]
        public void When_stopping_a_running_task()
        {
            var executeWasRun = false;
            var startWasRun = false;
            var stopWasRun = false;
            BehaviourTree behaviourTree = new BehaviourTree();
            behaviourTree.Root = new ActionBehaviour(
                () =>
                {
                    executeWasRun = true;
                    return RunningState.Failure;
                },
                () =>
                {
                    startWasRun = true;
                },
                () =>
                {
                    stopWasRun = true;
                });
            behaviourTree.SetWasRunningPreviousTick(behaviourTree.Root);
            behaviourTree.Tick();
            executeWasRun.Should().BeTrue();
            startWasRun.Should().BeFalse();
            stopWasRun.Should().BeTrue();
        }
    }
}